package com.extrace.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.extrace.server.config.AjaxResult;
import com.extrace.server.mapper.CustomerMapper;
import com.extrace.server.pojo.Customer;
import com.extrace.server.service.CustomerService;
import com.extrace.server.service.RoleService;
import com.sun.org.apache.xerces.internal.impl.dtd.models.CMAny;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import javax.persistence.Column;
import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@CrossOrigin
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private RoleService roleService;
    @Autowired
    WebApplicationContext applicationContext;

    @Resource
    CustomerMapper customerMapper;

    @PostMapping("/Domain/Customer/getCustomerList")
    public AjaxResult getCustomerList(@RequestBody Map<String, Object> params){
        LambdaQueryWrapper<Customer> LambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(params.containsKey("name") && StringUtils.hasLength(String.valueOf(params.get("name")))){
            LambdaQueryWrapper.like(Customer::getName, params.get("name"));
        }

        if(params.containsKey("address") && StringUtils.hasLength(String.valueOf(params.get("address")))){
            LambdaQueryWrapper.like(Customer::getAddress, params.get("address"));
        }

        if(params.containsKey("telCode") && StringUtils.hasLength(String.valueOf(params.get("telCode")))){
            LambdaQueryWrapper.eq(Customer::getTelCode, params.get("telCode"));
        }

        if(params.containsKey("regionCode") && StringUtils.hasLength(String.valueOf(params.get("regionCode")))){
            LambdaQueryWrapper.eq(Customer::getRegionCode, params.get("regionCode"));
        }

        LambdaQueryWrapper.eq(Customer::getRegistered, 1);

        List<Customer> customers = customerMapper.selectList(LambdaQueryWrapper);
        return AjaxResult.success(customers);
    }

    @PostMapping("/Domain/Customer/updateInfo")
    public AjaxResult updateInfo(@RequestBody Customer c){
        return AjaxResult.success(customerMapper.updateById(c));
    }


    /**
     *  管理员添加用户
     * @param c
     * @return
     */
    @PostMapping("/Domain/Customer/addInfo")
    public AjaxResult addInfo(@RequestBody Customer c){
        c.setRegistered(1);
        return AjaxResult.success(customerMapper.insert(c));
    }

    /**
     *  管理员添加用户
     * @param params
     * @return
     */
    @PostMapping("/Domain/Customer/deleteInfo")
    public AjaxResult deleteInfo(@RequestBody Map<String, Object> params){
        Integer id = (Integer) params.get("id");
        return AjaxResult.success(customerMapper.deleteById(id));
    }


    @GetMapping("/getParam")
    public String getParam(){

        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 拿到Handler适配器中的全部方法
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : methodMap.keySet()){

            Set<String> urlSet = info.getPatternsCondition().getPatterns();
            // 获取全部请求方式
            Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
            System.out.println(Methods.toString());
            for (String url : urlSet){
                // 加上自己的域名和端口号，就可以直接调用
                urlList.add("http://localhost:XXXX"+url);
            }
        }
        return urlList.toString();
    }
    @GetMapping(value = "/Misc/Customer/getCustomerListByName")
    public List<Customer> getCustomerListByName(String name, HttpServletResponse response) {
        response.addHeader("state", "read_success");
        return customerService.findByName(name);
    }
    @GetMapping(value = "/Misc/Customer/getCustomerListByTelCode/{telCode}")
    public Customer getCustomerListByTelCode(@PathVariable String telCode, HttpServletResponse response) {
        System.out.println(telCode);
        response.addHeader("state", "read_success");
        return customerService.findByTelCode(telCode);
    }
    @GetMapping(value = "/Misc/Customer/getCustomerInfo/{id}")
    public Customer getCustomerInfo(@PathVariable int id, HttpServletResponse response) {
        response.addHeader("state", "read_success");
        return customerService.findById(id);
    }
    @GetMapping(value = "/Misc/Customer/deleteCustomerInfo/{id}")
    public Customer deleteCustomerInfo(@PathVariable int id, HttpServletResponse response) {
        response.addHeader("state", "delete_success");
        return customerService.deleteById(id);
    }
    @GetMapping(value = "/Misc/Customer/saveCustomerInfo")
    public Customer deleteCustomerInfo(@RequestBody Customer c, HttpServletResponse response) {
        response.addHeader("state", "create_success");
        c.setRegistered(1);
        return customerService.save(c);
    }
}
